/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.CloseButtonCallback;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.WindowClosedCallback;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

/**
 *
 * @author JKorreck
 */
public class ItemActionPanel extends Panel{

    MyItems parent;
    ConfirmDeleteItemPage deletePage = new ConfirmDeleteItemPage();
    SelectModalWindow deleteWindow;
    ItemEditModalWindow editWindow;
    String selectedID = "";

    /**
     * @param id
     *            component id
     * @param model
     *            model for contact
     * @param parent
     */
    public ItemActionPanel(String id, IModel model, MyItems parent) {
        super(id, model);
        this.parent = parent;


        add(new Link("select") {

            public void onClick() {
//                    selected = (getParent()).getModelObject();
//                this.getParent().getDefaultModelObject();
                ItemActionPanel.this.parent.setSelected((MyItem) (this.getParent()).getDefaultModelObject());

            }
        });


        this.editWindow = new ItemEditModalWindow("editwindow", this.parent) {

            void onSelect(AjaxRequestTarget target, String selection) {


                close(target);

                ItemActionPanel.this.selectedID = "";
            }

            void onCancel(AjaxRequestTarget target) {
                // Handle Cancel action
//                resultLabel.setModelObject("ModalWindow cancelled.");
//                target.addComponent(resultLabel);
                // nothing
                close(target);
                ItemActionPanel.this.selectedID = "";
            }
        };


        add(editWindow);

        add(new AjaxLink("editLink") {

            @Override
            public void onClick(AjaxRequestTarget target) {

                add(ItemActionPanel.this.editWindow);
                MyItem sel = (MyItem) (this.getParent()).getDefaultModelObject();

                target.appendJavascript("Wicket.Window.unloadConfirmation = false;");
                ItemActionPanel.this.editWindow.setTextMessage("Edit Element " + sel.getName() + " (" + sel.getId() + ").");
                ItemActionPanel.this.editWindow.setTitleText("Edit of " + sel.getName());
                ItemActionPanel.this.selectedID = sel.getId();

                ((ItemEditContentPanel) ItemActionPanel.this.editWindow.get(ItemActionPanel.this.editWindow.getContentId())).setItemToEdit(sel.getId());
                ItemActionPanel.this.editWindow.show(target);
                
            }
        });

        this.deleteWindow = new SelectModalWindow("removewindow") {

            void onSelect(AjaxRequestTarget target, String selection) {

                MyItemManager.getInstance().removeItem(ItemActionPanel.this.selectedID);

                close(target);
                ItemActionPanel.this.selectedID = "";
//                setResponsePage(MyItems.class);
            }

            void onCancel(AjaxRequestTarget target) {
                // Handle Cancel action
//                resultLabel.setModelObject("ModalWindow cancelled.");
//                target.addComponent(resultLabel);
                // nothing
                close(target);
                ItemActionPanel.this.selectedID = "";
            }
        };

        add(deleteWindow);

        add(new AjaxLink("removeLink") {

            @Override
            public void onClick(AjaxRequestTarget target) {
                add(ItemActionPanel.this.deleteWindow);
                MyItem sel = (MyItem) (this.getParent()).getDefaultModelObject();

                target.appendJavascript("Wicket.Window.unloadConfirmation = false;");
                ItemActionPanel.this.deleteWindow.setTextMessage("Soll das Element " + sel.getName() + " (" + sel.getId() + ") wirklich gelöscht werden?");
                ItemActionPanel.this.deleteWindow.setTitleText("Löschen von " + sel.getName() + "?");
                ItemActionPanel.this.selectedID = sel.getId();
                ItemActionPanel.this.deleteWindow.show(target);
            }
        });
    }
}
